package cn.tedu.blog.server.service.impl;

import cn.tedu.blog.server.mapper.ArticleHistoryMapper;
import cn.tedu.blog.server.pojo.dto.ArticleHistoryDTO;
import cn.tedu.blog.server.pojo.dto.ArticleHistoryEditDTO;
import cn.tedu.blog.server.pojo.vo.ArticleHistoryVO;
import cn.tedu.blog.server.service.IArticleHistoryService;
import cn.tedu.blog.server.utils.JsonPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ArticleHistoryServiceImpl implements IArticleHistoryService {
    @Autowired
    private ArticleHistoryMapper articleHistoryMapper;
    
    
    @Override
    public void insertHistory(ArticleHistoryDTO articleHistoryDTO) {
        // 根据前端传递过来的参数, userId, articleId, 判断如果浏览过, 就做修改
        Long userId = articleHistoryDTO.getUserId();
        Long articleId = articleHistoryDTO.getArticleId();
        LocalDateTime localDateTime = articleHistoryMapper.getByUserIdAndArticleId(userId, articleId);
        log.info("{}", localDateTime);
        if (localDateTime != null) {  // 说明这篇文章该用户已经浏览过, 就修改浏览时间,不执行新增
            ArticleHistoryEditDTO articleHistoryEditDTO = new ArticleHistoryEditDTO();
            BeanUtils.copyProperties(articleHistoryDTO, articleHistoryEditDTO);
            // 为修改的浏览时间赋值
            LocalDateTime now = LocalDateTime.now();
            log.info("{}", now);
            articleHistoryEditDTO.setViewTime(now);
            articleHistoryMapper.updateViewTime(articleHistoryEditDTO);
            return;
        }
        // 代码走到此处说明当前用户指定文章没有浏览过
        // 前端传递过来文章id
        // 在控制层可以将SpringSecurity上下文中userId取出来
        // 此处需要补全属性
        LocalDateTime time = LocalDateTime.now();
        articleHistoryDTO.setViewTime(time);
        articleHistoryDTO.setGmtCreate(time);
        articleHistoryDTO.setGmtModified(time);
        int rows = articleHistoryMapper.insertHistory(articleHistoryDTO);
        // rows需要判断吗??????感觉不用
    }
    
    // 根据userId分页查询浏览历史
    @Override
    public JsonPage<ArticleHistoryVO> listHistory(Long userId, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        // userId通过controller得到传递过来
        List<ArticleHistoryVO> list = articleHistoryMapper.listHistory(userId);
        return JsonPage.restPage(new PageInfo<>(list));
    }

    // 根据用户id查询所有浏览数量
    @Override
    public int countHistory(Long userId) {
        int rows = articleHistoryMapper.countHistory(userId);
        System.out.println("===============rows:" + rows);
        return rows;
    }

    // 根据userId和页面展示条数查询历史浏览
    @Override
    public List<ArticleHistoryVO> listArticleHistory(Long userId, Integer pageSize) {
        List<ArticleHistoryVO> list = articleHistoryMapper.listArticleHistory(userId, pageSize);
        for (ArticleHistoryVO articleHistoryVO : list) {
            System.out.println("!!!!!!!!!!" + articleHistoryVO);
        }
        return list;
    }
    
}
